layui.define(['clipboard','jquery'], function(exports){
	var $ = layui.jquery;
	var tool = {
		// 物料类型格式化
		formatMaterialType: function(str, defaultVal='', model=1){
			if (model==1) {
				if (str == 'fabric') return "<span style='color:#009688'>面料</span>";
				else if (str == 'lining') return "<span style='color:#5FB878'>里料</span>";
				else if (str == 'accessories') return "<span style='color:#1E9FFF'>辅料</span>";
				else if (str == 'other') return "<span style='color:#a57a0d'>其它</span>";
				else return defaultVal;
			} else if (model==2) {
				if (str == 'fabric') return '<span class="layui-badge layui-bg-green">面</span> ';
				else if (str == 'lining') return '<span class="layui-badge layui-bg-cyan">里</span> ';
				else if (str == 'accessories') return '<span class="layui-badge layui-bg-blue">辅</span> ';
				else if (str == 'other') return '<span class="layui-badge" style="background-color: #a57a0d;">其它</span> ';
				else return defaultVal;
			}
		},
		// 加法运算
		bcadd: function(num1, num2, scale=2){
			num1 = num1 == '' ? 0 : parseFloat(num1);
			num2 = num2 == '' ? 0 : parseFloat(num2);
			return (num1+num2).toFixed(scale);
		},
		// 除法运算
		bcdiv: function(num1, num2, scale=2, defaultVal=0){
			num1 = num1 == '' ? 0 : parseFloat(num1);
			num2 = num2 == '' ? 0 : parseFloat(num2);
			if (num2==0) return defaultVal;
			return (num1/num2).toFixed(scale);
		},
		// 乘法运算
		bcmul: function(num1, num2, scale=2){
			num1 = num1 == '' ? 0 : parseFloat(num1);
			num2 = num2 == '' ? 0 : parseFloat(num2);
			return (num1*num2).toFixed(scale);
		},
		// 减法运算
		bcsub: function(num1, num2, scale=2){
			num1 = num1 == '' ? 0 : parseFloat(num1);
			num2 = num2 == '' ? 0 : parseFloat(num2);
			return (num1-num2).toFixed(scale);
		},
		//  浮点数转换
		parseFloat: function(num, scale=2) {
			if(num == '') return 0;
			else return (parseFloat(num)).toFixed(scale);
		},
		formatOrderOType(oType) {
			var orderOtype = {'FOB': 'FOB', 'CMT': 'CMT', 'CMT2': '新CMT'};
			return orderOtype[oType];
		},
		formatOrderType(type) {
			var orderOtype = {'normal': '返单', 'new': '首单', 'emergent': '紧急单'};
			return orderOtype[type];
		},
		// 向列表添加对象信息
		setDataToList(d, list) {
			x = JSON.parse(JSON.stringify(d));
			for(var i in list) {
				list[i]['p'] = x;
			}
			//console.log(list);
			return list;
		},
		// 显示下拉框
		showSelect(name, value, options, sclass='', otherKey='', sattr='') {
			sattr = sattr + ' data-key="'+otherKey+'"';
			var html = '<select name="'+name+'" id="'+name+'-'+otherKey+'" class="mimo-select '+sclass+'" '+sattr+' lay-ignore>';
			for(var i in options) {
				if((options[i]).hasOwnProperty('k') && (options[i]).hasOwnProperty('v')){
					html = html + '<option value="'+options[i]['k']+'" '+(value==options[i]['k'] ? "selected":"")+'>'+options[i]['v']+'</option>';
				} else {
					html = html + '<option value="'+i+'" '+(value==i ? "selected":"")+'>'+options[i]+'</option>';
				}
			}
			html = html+'</select>';
			return html;
		},
		// 复制到剪贴板
		copyText(msg) {
			if ( $(".copybtn").length > 0 ) {
			}else {
				$("body").append('<button class="copybtn" style="display:none;"></button>');
			}
			var clipboard = new Clipboard('.copybtn', {
				text: function() {
					return msg;
				}
			});

			clipboard.on('success', function(e) {
				layer.msg("已复制到剪贴板", {icon: 1});
			});

			clipboard.on('error', function(e) {
				layer.msg("复制失败啦，骚年", {icon: 1});
			});

			$(".copybtn").trigger("click");
		},
		formatBool(val) {
			if (val == 1) return '<span class="layui-badge layui-bg-green">是</span>';
			else if(val == 0) return '<span class="layui-badge">否</span>';
			else return '';
		},
		formatOverStatus(val) {
			if (val > 0) return '<span class="layui-badge layui-bg-green">Y</span>';
			else return '<span class="layui-badge">N</span>';
		},
		formatCaichuangType(val) {
			if (val == 2) return '<span class="layui-badge layui-bg-green">电脑裁剪</span>';
			else return '<span class="layui-badge">手工裁剪</span>';
		},
		formatCheckclothShrinkType(val) {
			if (val == 2) return '<span class="layui-badge layui-bg-red">缩水</span>';
			else return '<span class="layui-badge">验布</span>';
		},
		formatOnline(val) {
			if (val == 1) return '<i class="layui-icon layui-icon-chart-screen" title="在线" style="color: green;"></i>';
			else return '<i class="layui-icon layui-icon-chart-screen" title="离线"></i>';
		},
		getQueryString(string, name, defaultVal='') {
			var theRequest = new Object();
			strs = string.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			}
			
			if (name == '') return theRequest;
			if (typeof(theRequest[name]) != 'undefined') {
				return theRequest[name];
			} else return defaultVal;
		}
	};
  
	//输出tool接口
	exports('tool', tool);
});  